package com.wzh.store.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.store.entity.Cart;
import com.wzh.store.entity.Product;
import com.wzh.store.mapper.CartMapper;
import com.wzh.store.mapper.ProductMapper;
import com.wzh.store.service.ICartService;
import com.wzh.store.service.ex.AccessDeniedException;
import com.wzh.store.service.ex.CartNotFindException;
import com.wzh.store.service.ex.InsertException;
import com.wzh.store.service.ex.UpdateException;
import com.wzh.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author wzh
 * @create 2022/4/30 14:03
 * @Description：
 */
@Service
public class CartServiceimpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public void addToCart(Integer uid,Integer pid,Integer amount,String username) {
        Cart result = cartMapper.findByUidAndPid(uid, pid);
        Date date = new Date();
        if (result==null){
            Cart cart = new Cart();
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            cart.setCreatedUser(username);
            Product pro = productMapper.findById(pid);
            cart.setPrice(pro.getPrice());
            cart.setCreatedTime(date);
            cart.setModifiedTime(date);
            cart.setModifiedUser(username);
            int insert = cartMapper.insert(cart);
            if (insert!=1){
                throw new InsertException("加入购物车失败,插入失败，插入数据时产生未知的异常");
            }
        }else {
            Integer cid = result.getCid();
            Integer num=result.getNum()+amount;
            Integer row = cartMapper.updateNumByCid(cid, num, username, date);
            if (row!=1){
                throw new UpdateException("加入购物车失败，更新时产生未知的异常");
            }
        }
    }

    @Override
    public List<CartVo> getCartByUid(Integer uid) {
        List<CartVo> list = cartMapper.findVoByUid(uid);
        return list;
    }

    @Override
    public Integer addNum(Integer uid, Integer cid, String username) {
        Cart result = cartMapper.findByCid(cid);
        if (result==null){
            throw new CartNotFindException("数据不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        Integer num=result.getNum()+1;
        Integer row = cartMapper.updateNumByCid(cid, num, username, new Date());
        if (row!=1){
            throw new UpdateException("用户更新时产生异常，更新失败");

        }
//        返回新的购物车的总量
        return num;
    }

    @Override
    public List<CartVo> getVoByCid(Integer[] cids, Integer uid) {
        List<CartVo> list = cartMapper.findVoByCid(cids);
        Iterator<CartVo> it = list.iterator();
        while (it.hasNext()){
            CartVo cartVo = it.next();
            if (!cartVo.getUid().equals(uid)){
                list.remove(cartVo);
            }

        }
        return list;
    }
}
